package net.jlibrary.curbparking.action;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import net.jlibrary.common.CoreConstants;
import net.jlibrary.common.core.annotation.Token;
import net.jlibrary.common.core.entity.Pagination;
import net.jlibrary.common.core.entity.PaginationParam;
import net.jlibrary.curbparking.action.vo.DeviceVo;
import net.jlibrary.curbparking.service.IDeviceManager;
import net.jlibrary.group.domain.ParkingPlace;

@RequestMapping("/device")
@RestController
public class DeviceAdminAction {

	@Autowired
	private IDeviceManager deviceManager;

	@RequestMapping(value = "/deviceAdminView", method = { RequestMethod.GET })
	public ModelAndView view() {
		return new ModelAndView("/curbparking/deviceAdminView");
	}

	@RequestMapping(value = "/deviceAdminViewList", method = { RequestMethod.POST })
	public Pagination lists(DeviceVo deviceVo, PaginationParam param) {
		Pagination pagination = deviceManager.listsPage(deviceVo, param);
		return pagination;
	}

	@RequestMapping(value = "/deviceFormInit", method = { RequestMethod.GET })
	public DeviceVo addFormInit() {
		DeviceVo deviceVo = new DeviceVo();
		return deviceVo;
	}

	@RequestMapping(value = "/detailDevice", method = { RequestMethod.GET })
	public DeviceVo detail(DeviceVo deviceVo) {
		deviceVo = deviceManager.get(deviceVo);
		return deviceVo;
	}

	@RequestMapping(value = "/saveDevice", method = { RequestMethod.POST })
	@Token
	public ModelMap save(@ModelAttribute("vm") DeviceVo deviceVo, @Token String token, BindingResult result) {
		deviceManager.save(deviceVo);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/updateDevice", method = { RequestMethod.POST })
	@Token
	public ModelMap update(@ModelAttribute("vm") DeviceVo deviceVo, @Token String token, BindingResult result) {
		deviceManager.update(deviceVo);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/deleteDevice", method = { RequestMethod.POST })
	public ModelMap delete(Long id) {
		DeviceVo deviceVo = new DeviceVo();
		deviceVo.setId(id);
		deviceManager.delete(deviceVo);
		ModelMap map = new ModelMap();
		map.put("success", true);
		return map;
	}

	@RequestMapping(value = "/parkJsonData", method = { RequestMethod.GET })
	public List<ModelMap> parkJsonData() {
		List<ParkingPlace> places = deviceManager.parkingPlaces();
		List<ModelMap> maps = new ArrayList<ModelMap>();
		for (ParkingPlace place : places) {
			ModelMap map = new ModelMap();
			map.put(CoreConstants.VIEW_SELECT_LABEL_VALUE, place.getId());
			map.put(CoreConstants.VIEW_SELECT_LABEL_TEXT, place.getFullName());
			maps.add(map);
		}
		return maps;
	}

}
